package com.yylending.las.lending;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.Loader;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.airbnb.lottie.LottieAnimationView;
import com.trello.rxlifecycle.components.support.RxAppCompatActivity;
import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yylending.las.Constants;
import com.yylending.las.R;
import com.yylending.las.YJKD;
import com.yylending.las.authority.login.LoginActivity;
import com.yylending.las.base.BaseFragment;
import com.yylending.las.common.widgets.RedDotView;
import com.yylending.las.main.MainController;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 加载动画
 * Created by ying_he on 2017/7/4.<br>
 * Copyright  2017 www.yylending.com. All Rights Reserved.<br><br>
 */

public class LoadingFragment extends BaseFragment {

    static final String TAG = "LoadingFragment";

    static final String ARG_NO_NETWORK = "arg_no_network";

    @BindView(R.id.title_left_iv)
    ImageView mTitleLeftIv;
    @BindView(R.id.title_tv)
    TextView mTitleTv;
    @BindView(R.id.title_right_tv)
    TextView mTitleRightTv;
    @BindView(R.id.title_right_iv)
    ImageView mTitleRightIv;
    @BindView(R.id.layout_title_bar)
    RelativeLayout mLayoutTitleBar;
    @BindView(R.id.loading_view)
    LottieAnimationView mLoadingView;
    @BindView(R.id.page_loading_ll)
    LinearLayout mPageLoadingLl;
    Unbinder unbinder;

    @BindView(R.id.no_network_layout)
    RelativeLayout mNoNetworkLayout;

    boolean mShowNoNetwork = false;
    /**
     * 是否可点击重新加载
     */
    boolean mCanReload = true;
    @BindView(R.id.red_dot_view)
    RedDotView mRedDotView;

    public static BaseFragment newInstance(boolean showNoNetwork) {
        BaseFragment fragment = new LoadingFragment();
        Bundle args = new Bundle();
        args.putBoolean(ARG_NO_NETWORK, showNoNetwork);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onLoadFinished(Loader<Presenter> loader, Presenter data, int placeholder) {

    }

    @Override
    public Loader<Presenter> onCreateLoader(int id, Bundle args) {
        return null;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            mShowNoNetwork = bundle.getBoolean(ARG_NO_NETWORK, false);
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        EventAgent.register(this);
    }

    @Override
    public void onDetach() {
        EventAgent.unregister(this);
        super.onDetach();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        mCanReload = !hidden;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_loading, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mTitleLeftIv.setImageResource(R.drawable.ic_home_help);
        mTitleTv.setText(R.string.tab_lend);
        mTitleRightIv.setImageResource(R.drawable.ic_message_white);
        if (mShowNoNetwork) {
            showNoNetwork();
        } else {
            showLoadingAnim();
        }
        MainController.initRedDot(mRedDotView);
    }

    private void showNoNetwork() {
        mLoadingView.cancelAnimation();
        mLoadingView.clearAnimation();
        mPageLoadingLl.setVisibility(View.GONE);
        mNoNetworkLayout.setVisibility(View.VISIBLE);
    }

    private void showLoadingAnim() {
        mNoNetworkLayout.setVisibility(View.GONE);
        mPageLoadingLl.setVisibility(View.VISIBLE);
        mLoadingView.playAnimation();
    }

    @Override
    public void onDestroyView() {
        mLoadingView.cancelAnimation();
        mLoadingView.clearAnimation();
        unbinder.unbind();
        super.onDestroyView();
    }

    @OnClick({R.id.no_network_layout, R.id.title_left_iv, R.id.title_right_iv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_left_iv:
                EventAgent.post(new QuotaEvent.PageJumpEvent(QuotaEvent.PageJumpEvent.QUOTA_JUMP_HELP_PAGE, Constants.PAGE_NO_MAIN_ACTIVITY));
                break;
            case R.id.title_right_iv:
                if (!YJKD.login()) {
                    LoginActivity.start((RxAppCompatActivity) getActivity());
                    return;
                }
                EventAgent.post(new QuotaEvent.PageJumpEvent(QuotaEvent.PageJumpEvent.QUOTA_JUMP_MESSAGE_PAGE, Constants.PAGE_NO_MAIN_ACTIVITY));
                MainController.hideRedDot(mRedDotView);
                break;
            case R.id.no_network_layout:
                if (mCanReload)
                    EventAgent.post(new QuotaEvent.RefreshStatusEvent(QuotaEvent.Event.QUOTA_REFRESH, null));
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(QuotaEvent.ShowLoadingEvent event) {
        switch (event.id) {
            case QuotaEvent.Event.QUOTA_SHOW_LOADING:
                mShowNoNetwork = event.noNetwork;
                if (mShowNoNetwork) {
                    showNoNetwork();
                } else {
                    showLoadingAnim();
                }
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onRedDotShowEvent(QuotaEvent.ShowRedDotEvent event){
        mRedDotView.setVisibility(View.VISIBLE);
    }
}
